﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace EightBeatsAPI.Controllers
{
    [RoutePrefix("Audio")]
    [AllowAnonymous]
    public class AudioController : ApiController
    {
                /// <summary>
        /// [REQUIRES AUTHENTICATION] Update Company Status
        /// </summary>
        [Route("File/Upload")]
        [HttpPost]
        public async Task<IHttpActionResult> Post([FromBody]string fileBase64Str, string contentType)
        {
            Guid fileId = Guid.NewGuid();

            await Task.Run(() =>
            {

            });

            return Ok(fileId);
        }


        [Route("File/Process")]
        [HttpGet]
        public async Task<HttpResponseMessage> Get(Guid id)
        {
            var res = new HttpResponseMessage();

            byte[] fileStream = new byte[]{};

            await Task.Run(() =>
            {
                //res.Content.Headers.ContentType = new MediaTypeHeaderValue("audio/midi");
                res.Content = new ByteArrayContent(fileStream);
            });

            return res;
        }


        #region Helpers
        public static string Unzip(byte[] bytes)
        {
            using (var msi = new MemoryStream(bytes))
            using (var mso = new MemoryStream())
            {
                using (var gs = new GZipStream(msi, CompressionMode.Decompress))
                {
                    //gs.CopyTo(mso);
                    CopyTo(gs, mso);
                }

                return Encoding.UTF8.GetString(mso.ToArray());
            }
        }

        public static void CopyTo(Stream src, Stream dest)
        {
            byte[] bytes = new byte[4096];

            int cnt;

            while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0)
            {
                dest.Write(bytes, 0, cnt);
            }
        } 
        #endregion

    }
}
